import type { OptionTypes, ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import type { MemberRecordTableItem, CreateMemberRecordType, RenewalType } from './types.d';
import request from '@/utils/request';

export async function queryMemberRecords(data: any, sort: any) {
  const res: ResponseData<{ total: number; data: MemberRecordTableItem[] }> = await request(
    '/api/member/v1.0.0/records',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createMemberRecord(params: CreateMemberRecordType) {
  const res = await request('/api/member/v1.0.0/record', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}

export async function renewalMemberRecord(params: RenewalType) {
  const res = await request('/api/member/v1.0.0/record', {
    method: 'PUT',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '续费成功',
    };
  return { data: res?.data, success: false, message: res?.message || '续费失败' };
}

export async function queryMemberCardNames(cardName?: string) {
  const res: ResponseData<OptionTypes[]> = await request('/api/member/v1.0.0/cardNames', {
    params: {
      cardName,
    },
  });
  if (res?.code !== SUCCESS_CODE) {
    return [];
  }
  return res.data;
}

export async function cancelMember(data: { id: string; reason: string }) {
  const res = await request('/api/member/v1.0.0/cancel', {
    method: 'PUT',
    data,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '作废成功',
    };
  return { data: res?.data, success: false, message: res?.message || '作废失败' };
}
